﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PitOfDespair.Models
{
	using PitOfDespair.Models.Character;

	public class CombatModel
	{
		readonly int _seed;

		readonly Random _rnd;

		public CombatModel(BaseCharacter player, BaseCharacter opponent,int seed)
		{
			_seed = seed;
			_rnd = new Random(_seed);
			Player = player;
			Opponent = opponent;
		}

		public BaseCharacter Player { get; private set; }

		public BaseCharacter Opponent { get; private set; }

		public void ResolveRound()
		{
			this.ResolveAttack(Player, Opponent);
			this.ResolveAttack(Opponent, Player);
		}

		public void ResolveAttack(BaseCharacter attacker, BaseCharacter defender)
		{
			var attackerPower = attacker.GetSkill(SkillName.Melee_Offense).AdjustedBaseValue;
			var defenderDefense = defender.GetSkill(SkillName.Melee_Defense).AdjustedBaseValue;
			var chanceToHit = attackerPower == defenderDefense ? 0.55m : attackerPower > defenderDefense ? 0.75m : 0.25m;
			var didHit = _rnd.NextDouble() < (double)chanceToHit;
			if (!didHit) return;
			defender.GetVital(VitalName.Health).CurValue -= _rnd.Next(1, 5);
		}
	}
}